package controleur;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modele.Article;
import modele.Competition;
import modele.Config;
import modele.FacadeArticle;
import modele.FacadeMenu;
import modele.FacadeSite;
import modele.InfoSession;

/**
 * Servlet implementation class Recherche
 */
public class Recherche extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB private FacadeMenu fmenu;
	@EJB private FacadeSite fs;
	@EJB private FacadeArticle fa;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Recherche() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		//Session utilisateur
		HttpSession session = request.getSession(true);
		InfoSession infoSession = (InfoSession)session.getAttribute("infoSession");
		
		String pageInclue = "accueil.jsp";
		

		HashMap<String,List<Competition>> menu = fmenu.getMenu();
		
		String texteRecherche=request.getParameter("texteRecherche");	
		
		if (texteRecherche != null && texteRecherche != "") {
			
			int numeroPage = Integer.parseInt(request.getParameter("numeroPage"));
			
			int offset = (numeroPage-1)*Config.nbArticlesParPage;
			
			int nbArticles = fs.getNombreResultatsRechercheArticlesParTexte(texteRecherche);
			ArrayList<Article> articles = (ArrayList<Article>)fs.rechercherArticlesParTexte(offset, Config.nbArticlesParPage, texteRecherche);
			
			ArrayList<String> resumesArticles = new ArrayList<String>();
			
			for(Article article : articles){
				String resume = this.fa.getResume(article);
				
				resumesArticles.add(resume);
			}
			
			request.setAttribute("resumesArticles", resumesArticles);
			request.setAttribute("listeArticles", articles);
			request.setAttribute("nbPages", (int)Math.ceil((double)nbArticles/Config.nbArticlesParPage));
			request.setAttribute("nbArticles", nbArticles);
			pageInclue = "recherche.jsp";
		
		}
		if (pageInclue.equals("accueil.jsp")) {

			request.getRequestDispatcher("Site?page=accueil").forward(request,
					response);

		} else {
			request.setAttribute("idsSports", this.fs.getIdsSports());
			request.setAttribute("formatDate", Config.sdf);
			request.setAttribute("infoSession", infoSession);
			request.setAttribute("Menu", menu);
			request.setAttribute("pageInclue", pageInclue);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
